Get premium membership and access questions with answers, video lessons as well as revision papers.
#include
#include
#include
using namespace std;
class strtype {
char *p;
int len;
public:
strtype();
strtype (char *s, int l);
char *getstring() {return p;}
int getlength() {rn len;}
};
strtype::strtype()
{
p = new char[255];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
*p = '\0'; // null string
len =strtype::strtype(char *s, int l)
{
if(strlen(s) >= l) {
cout << "Allocating too little memory!\n";
exit(1);
}
p = new char [l];
if(!p) {< "Allocation error\n";
exit(1);
}
strcpy(p, s);
len = l;
}
int main()
{
strtype s1;
strtype s2("This is a test", 100);
cout << "s1: " <cout <cout << "s2: " << s2.getstring() << " -Length: ";
cout << s2.getlength() << '\n';
return 0;
}.
Githiari answered the question on May 29, 2018 at 17:54
- Explain what is wrong with the following program and then fix it.
// This program contains an error.
#include
#include
using namespace std;
class myclass {
int *p;
public:
myclass(int i);
~myclass()...(Solved)
Explain what is wrong with the following program and then fix it.
// This program contains an error.
#include
#include
using namespace std;
class myclass {
int *p;
public:
myclass(int i);
~myclass() { delete p;}
friend int getval(myclass o);
};
myclass::myclass(int)
{
p = n;
if(!p) {
cout << "Allocation error\n";
exit(1);
}
*p = i;
}
int getval(myclass o)
{
return *o.p; // get value
}
int main()
{
myclass (2);
cout << getval (a) << " " <cout <<"\n";
cout << getval(a) << " " << getval(b);
return 0;
}
Date posted: May 29, 2018. Answers (1)
- Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to...(Solved)
Imagine a situation in which two classes, called pr1 and pr2, shown below, share one printer.Further imagine that other parts of your program need to when the printer is in use by an object of either of these two classes. Create a function in C++ called inuse() that returns true when the printer is being used by either and false otherwise. Make this function a friend of both pr1 and pr2.
class pri1 {
int printing; //...
public:
pr1 () {printing = 0}
void set_print (int status) {printing = status;}
// ...
};
class pr2 {
int printing;
// ...
public:
pr2 () {printing = 0;}
void set_prt status) {printing = status; }
// ...
};
Date posted: May 29, 2018. Answers (1)
- Given this class;
class planet {
int moons;
double dist_from_sun; // in miles
double diameter;
double mass;
public:
//...
double get_ () { return disfrom_sun; }
};
Create a function called light() using c++...(Solved)
Given this class;
class planet {
int moons;
double dist_from_sun; // in miles
double diameter;
double mass;
public:
//...
double get_ () { return disfrom_sun; }
};
Create a function called light() using c++ language that takes as an argument an object of type planet and returns the number of secos that it takes light from the sun to reach the planet.(Assume that light travels at 186,000 miles per second and that dist_from_sun is specified in miles.)
Date posted: May 29, 2018. Answers (1)
- When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class...(Solved)
When an object of a derived class is assigned to another object of the same derived class, is the data associated with the base class copied? To find out, use the following two classes and write a program that demonstrates what happens.
class base {
int a;
public:
void load_a(int n) { a= n; }
int ge() { return a;}
};
class derived : public base {
int b;
public:
void load_b(int n) {b=n;}
int get_b() {return b;}
};
Date posted: May 29, 2018. Answers (1)
- Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this...(Solved)
Using the stack class, write a function called loadstack() that returns a stack that is already loaded with letters of the alphabet (a-z.). Assign this stack to another object in the calling routine and prove that it contains the alphabet. Be sure to change the stack size so it is large enough to hold the alphabet.
Date posted: May 29, 2018. Answers (1)
- Explain five good programming practices(Solved)
Explain five good programming practices.
Date posted: May 29, 2018. Answers (1)
- List five examples of softwares(Solved)
This is a question based on Introduction to Computer Systems unit in Computer science course
Date posted: May 26, 2018. Answers (1)
- List three categories of operating systems(Solved)
List three categories of operating systems.
Date posted: May 26, 2018. Answers (1)
- As a programmer, what is the significance of the Integrated Development Environment(IDE) in programming?(Solved)
As a programmer, what is the significance of the Integrated Development Environment(IDE) in programming?
Date posted: May 26, 2018. Answers (1)
- As a programmer, briefly explain why comments are used when writing a program(Solved)
As a programmer, briefly explain why comments are used when writing a program.
Date posted: May 26, 2018. Answers (1)
- Define the following terms as used in computer programming: (a.) A compiler (b.) A debugger (c.) An algorithm(Solved)
Define the following terms as used in computer programming: (a.) A compiler (b.) A debugger (c.) An algorithm
Date posted: May 26, 2018. Answers (1)
- What is computer programming?(Solved)
What is computer programming?
Date posted: May 26, 2018. Answers (1)
- Using an example, differentiate a code from a program(Solved)
Using an example, differentiate a code from a program.
Date posted: May 26, 2018. Answers (1)
- Explain three techniques of detecting deadlocks in a computer system(Solved)
Explain three techniques of detecting deadlocks in a computer system.
Date posted: May 16, 2018. Answers (1)
- Explain common types of computer networks(Solved)
Explain common types of computer networks.
Date posted: May 16, 2018. Answers (1)
- What is social Engineering?(Solved)
What is social Engineering?
Date posted: May 16, 2018. Answers (1)
- Explain characteristics of information security within an organization? (Solved)
Explain characteristics of information security within an organization?
Date posted: May 16, 2018. Answers (1)
- Giving examples explain threats to data security(Solved)
Giving examples explain threats to data security.
Date posted: May 16, 2018. Answers (1)
- What is a cyber crime?(Solved)
What is a cyber crime?
Date posted: May 16, 2018. Answers (1)
- Describe the difference between an internal and an external hyperlink(Solved)
Describe the difference between an internal and an external hyperlink
Date posted: May 14, 2018. Answers (1)